﻿//
// Plugins.cs: Defines how to plug in to the S9Pal application.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using S9PalCommon;

namespace LastFMPhotoCrawler
{
    /// <summary>
    /// Defines how to plug in to the S9Pal application.
    /// </summary>
    public class Plugin : IPlugin
    {
        #region Fields

        private string name = "Last.fm Photo Crawler";
        private string description = "Crawls Last.fm website and automatically downloads artist photos.";
        private string author = "Michael Goethe";
        private string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        private List<IMainControl> mainControls = null;
        private List<ISettingsControl> settingsControls = null;
        private List<Settings> settings = null;
        private List<Syncer> syncers = null;
        private List<Worker> workers = null;
        private ArtistPhotoWatcher watcher;
        private LastFMPhotoCrawler crawler;
        private ArtistPhotoSyncer artistPhotoSyncer;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the title of the plugin.
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
        }
        /// <summary>
        /// Gets a description of what the plugin does.
        /// </summary>
        public string Description
        {
            get
            {
                return description;
            }
        }
        /// <summary>
        /// Gets the author of the plugin.
        /// </summary>
        public string Author
        {
            get
            {
                return author;
            }
        }
        /// <summary>
        /// Gets the plugin's version.
        /// </summary>
        public string Version
        {
            get
            {
                return version;
            }
        }

        /// <summary>
        /// Gets the controls that are to be added to the main form.
        /// </summary>
        public List<IMainControl> MainControls 
        {
            get
            {
                return mainControls;
            }
        }
        /// <summary>
        /// Gets the controls that are to be added to the settings form.
        /// </summary>
        public List<ISettingsControl> SettingsControls
        {
            get
            {
                return settingsControls;
            }
        }
        /// <summary>
        /// Gets the settings objects that are sent to the host application.
        /// </summary>
        public List<Settings> Settings 
        {
            get
            {
                return settings;
            }
        }
        /// <summary>
        /// Gets the syncer objects that are sent to the host application.
        /// </summary>
        public List<Syncer> Syncers
        {
            get
            {
                return syncers;
            }
        }
        /// <summary>
        /// Gets the worker objects that are sent to the host application.
        /// </summary>
        public List<Worker> Workers 
        {
            get
            {
                return workers;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Initializes the plugin and its components.
        /// </summary>
        public void Initialize()
        {
            // Instantiate watcher.
            watcher = new ArtistPhotoWatcher();

            // Add controls to main controls list.
            mainControls = new List<IMainControl>();
            mainControls.Add(new ArtistPhotosControl(watcher));

            // Add workers to workers list.
            workers = new List<Worker>();
            crawler = new LastFMPhotoCrawler();
            workers.Add(crawler);

            // Add syncers to syncers list.
            syncers = new List<Syncer>();
            artistPhotoSyncer = new ArtistPhotoSyncer();
            syncers.Add(artistPhotoSyncer);

            // Add controls to settings controls list.
            settingsControls = new List<ISettingsControl>();
            settingsControls.Add(new LastFMPhotoCrawlerSettingsControl());
        }

        /// <summary>
        /// Disposes the plugin and its components.
        /// </summary>
        public void Dispose()
        {
            if (crawler.Status == WorkerStatus.Active)
            {
                crawler.Pause();
                while (crawler.Status != WorkerStatus.Paused)
                {
                    Thread.Sleep(50);
                }
            }

            if (artistPhotoSyncer.Status == WorkerStatus.Active)
            {
                artistPhotoSyncer.Stop();
                while (artistPhotoSyncer.Status != WorkerStatus.Stopped)
                {
                    Thread.Sleep(50);
                }
            }

            Global.Settings.SaveSettings();
        }

        #endregion
    }
}
